home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM A / PD-ROM A.iso / Education / Math / Matrix.0 / Read⁄Write Source / write binary.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-12  |  6.3 KB  |  309 lines  |  [TEXT/KAHL]

  1. /*
  2.     WRITE BINARY.c
  3.     
  4.     by:      John A. Schlack
  5.     Date:    July 12, 1990
  6.     
  7.         This is a procedure designed to help read the files created by Matrix 
  8.     Manipulation.  It is taken from the source code for Matrix Manipulation 
  9.     with certain procedures commented out.  This should help one visualize how
  10.     the data should be read as this is the procedure that Matrix Manipulation 
  11.     uses to write the matrix data file.
  12.         If you want to know how to write a binary file, see READ BINARY.c.
  13.     You can modify these two files to read and write ASCII files.  These are
  14.     only meant as a guide line, and are not meant to be simply placed into
  15.     a program to create and read the Matrix Manipulation files.
  16. */
  17.  
  18.  
  19. #define RETURN_CHAR    0x0D
  20.  
  21. #define TRANSPOSE_FUNC            1
  22. #define EIGEN_FUNC                2
  23. #define SOLVE_SYS_FUNC            3
  24. #define INVERSE_FUNC            4
  25. #define SET_DEFAULT                5
  26.  
  27.  
  28. extern struct filePreferences
  29. {
  30.     Boolean        saveASCII;
  31. } gFilePrefs;
  32.  
  33. extern    double    **gA;
  34. extern    double    *gD;
  35. extern     int        gN;
  36.  
  37.  
  38. /* -------------------------------------------------------------------- */
  39.  
  40. void WriteNoHeaderFileData( reply )
  41.  
  42. SFReply reply;
  43.  
  44. /*
  45.     WriteASCIILine is a procedure (not provided here) that accepts a string and
  46.             writes the ASCII data of the string to a file
  47.     fileErrors is a procedure to display an alert indicating the type of error
  48.             that occurred;  it is not provided here
  49.     gFunc is a variable that stores the type of function written
  50.     gN is the matrix size (N x N)
  51.     gFilePrefs.saveASCII stores a flag which states whether the data should
  52.             be saved in ASCII or binary format
  53.     
  54.     PROCEDURE:
  55.     1.    create a new file
  56.     2.    open the file
  57.     3.    write the number corresponding to the function type (detailed in 
  58.             Save File Templates of the included text and #defined above)
  59.     4.    write the size of the matrix (N x N;  note: only one value is written)
  60.     5.    depending on the function, write either matrix, vector, or both types
  61.             of data
  62.     6.    set end of file
  63.     7.    close file and flush volume
  64. */
  65.  
  66. {
  67.     int     srcFile;
  68.     OSErr     err;
  69.     Boolean theErr = FALSE;
  70.     long    siz;
  71.     double    d;
  72.     char    tString[256];
  73.  
  74.     if (gFilePrefs.saveASCII == TRUE)
  75.     {
  76.         err = Create( reply.fName, reply.vRefNum, '????', 'TEXT'); /* IS TEXT IN THE RIGHT PLACE ? */
  77.     }
  78.     else
  79.     {
  80.         err = Create( reply.fName, reply.vRefNum, '????', '????');
  81.     }
  82.     if ((err == noErr) || (err == dupFNErr))
  83.     {
  84.         err = FSOpen( reply.fName, reply.vRefNum, &srcFile );
  85.         if (err == noErr)
  86.         {
  87. /* write function type */
  88.             if (gFilePrefs.saveASCII == TRUE)
  89.             {
  90.                 d = (double) gFunc;
  91.                 sprintf(tString, "%lf%c\0", d, RETURN_CHAR );
  92. /*                theErr = WriteASCIILine( (char *) tString, srcFile );*/
  93.             }
  94.             else
  95.             {
  96.                 siz = (long) sizeof(double);
  97.                 d = (double) gFunc;
  98.                 err = FSWrite( srcFile, &siz, &d );
  99.                 if (err != noErr)
  100.                 {
  101.                     theErr = TRUE;
  102. /*                    fileErrors( err );*/
  103.                 }
  104.             }
  105.             
  106. /* write matrix size */
  107.             if (gFilePrefs.saveASCII == TRUE)
  108.             {
  109.                 d = (double) gN;
  110.                 sprintf(tString, "%lf%c\0", d, RETURN_CHAR );
  111. /*                theErr = WriteASCIILine( (char *) tString, srcFile );*/
  112.             }
  113.             else
  114.             {
  115.                 siz = (long) sizeof(double);
  116.                 d = (double) gN;
  117.                 err = FSWrite( srcFile, &siz, &d );
  118.                 if (err != noErr)
  119.                 {
  120.                     theErr = TRUE;
  121. /*                    fileErrors( err );*/
  122.                 }
  123.             }
  124.             
  125.             if (theErr != TRUE)
  126.             {
  127.                 switch( gFunc )
  128.                 {
  129.                     case TRANSPOSE_FUNC :
  130.                     case INVERSE_FUNC :
  131.                             theErr = WriteFileMatrix( srcFile, gFunc );
  132.                             break;
  133.                     case EIGEN_FUNC :
  134.                             theErr = WriteFileVector( srcFile );
  135.                             if (theErr != TRUE)
  136.                             {
  137.                                 theErr = WriteFileMatrix( srcFile, gFunc );
  138.                             }
  139.                             break;
  140.                     case SOLVE_SYS_FUNC :
  141.                             theErr = WriteFileVector( srcFile );
  142.                             break;
  143.                     default:
  144.                             break;
  145.                 }
  146.             }
  147.  
  148.             GetFPos( srcFile, &siz );
  149.             SetEOF( srcFile, siz );
  150.             FSClose( srcFile );
  151.             FlushVol(0L, reply.vRefNum);
  152.         }
  153.         else
  154.         {
  155. /*            fileErrors(err);*/
  156.         }
  157.     }
  158.     else
  159.     {
  160. /*        fileErrors(err);*/
  161.     }
  162. }
  163.  
  164.  
  165. /* ---------------------------------------------------------------- */
  166.  
  167. Boolean WriteFileVector( srcFile )
  168.  
  169. int srcFile;
  170.  
  171. /*
  172.     gD is a global that stores the solution vector (array [0..N])
  173.     
  174.     PROCEDURE:
  175.     1.    loop through from 1 to N and write vector values to disk
  176. */
  177.  
  178. {
  179.     OSErr    err = noErr;
  180.     Boolean    theErr = FALSE;
  181.     register int    i = 1;
  182.     long    siz;
  183.     char    tString[256];
  184.     char    d;
  185.     
  186.     while ((err == noErr) && (i <= gN))
  187.     {
  188.         i += 1;
  189.         if (gFilePrefs.saveASCII != TRUE)
  190.         {
  191.             siz = (long) sizeof(double);
  192.             err = FSWrite( srcFile, &siz, &(gD[i]) );
  193.         }
  194.         else
  195.         {
  196.             sprintf(tString, "%lf%c\0", gD[i], RETURN_CHAR );
  197. /*            theErr = WriteASCIILine( (char *) tString, srcFile );*/
  198.             if (theErr == TRUE)
  199.             {
  200.                 i = gN + 1;
  201.             }
  202.         }
  203.     }
  204.     if (err != noErr)
  205.     {
  206. /*        fileErrors( err );*/
  207.         theErr = TRUE;
  208.     }
  209.     return( theErr );
  210. }
  211.  
  212.  
  213. /* ---------------------------------------------------------------- */
  214.  
  215. Boolean WriteFileMatrix( srcFile, theType )
  216.  
  217. int srcFile;
  218. int theType;
  219.  
  220. /*
  221.     gA is a global double ** that stores the matrix (it is an array [0..N][0..N])
  222.     
  223.     PROCEDURE:
  224.     1.    loop through from 1 to N for row with 1 to N for column nested in the original
  225.         loop and write matrix values to disk
  226. */
  227.  
  228. {
  229.     OSErr    err;
  230.     Boolean    theErr = FALSE;
  231.     register int    i;
  232.     register int    j;
  233.     long    siz;
  234.     char    tString[256];
  235.     char    d;
  236.     
  237.     if (gFilePrefs.saveASCII == TRUE)
  238.     {
  239.         if (gN <= 9)
  240.         {
  241.             d = '\t';
  242.         }
  243.         else
  244.         {
  245.             d = RETURN_CHAR;
  246.         }
  247.     }
  248.     
  249.     for (i=1; i<=gN; i++)
  250.     {
  251.         for (j=1; j<=gN; j++)
  252.         {
  253.             if (gFilePrefs.saveASCII != TRUE)
  254.             {
  255.                 siz = (long) sizeof(double);
  256.                 if (theType == EIGEN_FUNC)
  257.                 {
  258.                     err = FSWrite( srcFile, &siz, &(gA[j][i]) );    /* eigenvectors are stored as columns for a particular eigenvalue, not as rows */
  259.                 }
  260.                 else
  261.                 {
  262.                     err = FSWrite( srcFile, &siz, &(gA[i][j]) );
  263.                 }
  264.                 if (err != noErr)
  265.                 {
  266. /*                    fileErrors( err );*/
  267.                     theErr = TRUE;
  268.                     j = gN + 1;
  269.                     i = gN + 1;
  270.                 }
  271.             }
  272.             else
  273.             {
  274.                 if (theType == EIGEN_FUNC)
  275.                 {
  276.                     sprintf(tString, "%lf%c\0", gA[j][i], d );
  277. /*                    theErr = WriteASCIILine( (char *) tString, srcFile );*/
  278.                     if (theErr == TRUE)
  279.                     {
  280.                         i = gN + 1;
  281.                         j = gN + 1;
  282.                     }
  283.                 }
  284.                 else
  285.                 {
  286.                     sprintf(tString, "%lf%c\0", gA[i][j], d );
  287. /*                    theErr = WriteASCIILine( (char *) tString, srcFile );*/
  288.                     if (theErr == TRUE)
  289.                     {
  290.                         i = gN + 1;
  291.                         j = gN + 1;
  292.                     }
  293.                 }
  294.             }
  295.             if ((gFilePrefs.saveASCII == TRUE) && (gN <= 9))
  296.             {
  297.                 sprintf(tString, "%c\0", RETURN_CHAR );
  298. /*                theErr = WriteASCIILine( (char *) tString, srcFile );*/
  299.                 if (theErr == TRUE)
  300.                 {
  301.                     i = gN + 1;
  302.                     j = gN + 1;
  303.                 }
  304.             }
  305.         }
  306.     }
  307.     return( theErr );
  308. }
  309.